package com.hutao.sso.controller;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.sso.util.CookieUtils;
import com.hutao.sso.util.MD5;
import com.hutao.sso.util.TmFileUtil;


@Controller
@RequestMapping("/image")
public class ImageController1 {
	
	@Value("${config.resource.pic.url}")
	private String resourcePicUrl;
	
	@Value("${config.local.pic.url}")
	private String localPicUrl;
	
	
	@RequestMapping(value = "/seekMarkPicUrl")
	@ResponseBody
	public void markPic(HttpServletRequest request, HttpServletResponse response){
		double degree=0;
		String baseUrl=resourcePicUrl;
		String baseFileUrl=localPicUrl+"mark/";
		String currentUser = CookieUtils.getCookieValue(request, "TT_TOKEN");
		String mark=currentUser+"--呼吸内科";
		//文件名称
		String fileName=request.getParameter("fileName");
		String folderName=request.getParameter("folderName");
		String secret=request.getParameter("secret");
		
		//校验
		if(!MD5.md5(currentUser+folderName).equals(secret)){
			return;
		}
		
		InputStream in=null;
		OutputStream os =null;
		ServletOutputStream  out =null;
		InputStream fisq = null;
		try {
			in=download(baseUrl+fileName);
		
		Image srcImg = ImageIO.read(in);

		BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null),
				BufferedImage.TYPE_INT_RGB);
		
		Graphics2D graphics = buffImg.createGraphics();
		graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		graphics.drawImage(
				srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
				null);

		if (degree > 0) {
			graphics.rotate(Math.toRadians(0), (double) buffImg.getWidth() / 2,
					(double) buffImg.getHeight() / 2);
		}
		graphics.setColor(Color.RED);
		graphics.setFont(new Font("宋体", Font.BOLD, 100));

		float alpha = 0.8f;
		graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

		graphics.drawString(mark, buffImg.getWidth() / 2, buffImg.getHeight() / 2);
		graphics.dispose();
		
		//根据路径创建File
		File file2=new File(baseFileUrl+fileName);
		//创建文件夹
		TmFileUtil.createFloder(baseFileUrl+folderName);
		
		if(!file2.exists()) {
			file2.createNewFile();
		}
		//输出水印文件
		os = new FileOutputStream(file2);
		// 生成图片
		ImageIO.write(buffImg, "JPG", os);
		
		//获取response的输出流
		out = response.getOutputStream();
		//获取生成的文件IO流
		fisq = new FileInputStream(file2);
		//读取文件信息
		int count = 0;
		byte[] buffer = new byte[1024 * 8];
		while ((count = fisq.read(buffer)) != -1) {
			//写入
			out.write(buffer, 0, count);
			//刷入
			out.flush();
		}
		
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//关闭流
			try {
				if(fisq!=null)fisq.close();	
				if(out!=null)out.close();
				if(os!=null)os.close();
				if(in!=null)in.close();
			} catch (IOException e) {
					e.printStackTrace();
			}
			
		}
		
	}
	
	/**
     * 根据图片url下载文件至本地
     * @param urlString
     * @param savePath
     * @param fileName
     * @throws Exception
     */
    public  InputStream download(String urlString) {  
    	InputStream is = null;
    	try {
    		// 构造URL    
    		URL url = new URL(urlString);  
    		// 打开连接    
    		URLConnection con = url.openConnection();  
    		//设置请求超时为5s    
    		con.setConnectTimeout(5 * 1000);  
    		// 输入流    
    		is = con.getInputStream();  
		} catch (Exception e) {
			e.printStackTrace();
		}
        return is;  
    }  
  
	
	
}
